package com.xiang.thread;

import com.xiang.ThreadPoolMain;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MyThreadPoolExecutor extends ThreadPoolExecutor {


    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        // 在任务执行之前的操作
        log.info("Pre-processing beforeExecute");
        ThreadPoolMain.context.set("value-set-in-parent");
        super.beforeExecute(t, r);
    }

    @Override
    protected void afterExecute(Runnable r, Throwable t) {
        try {
            // 在任务执行之后的操作
            log.info("Post-processing afterExecute");
            ThreadPoolMain.context.remove();

        } finally {
            super.afterExecute(r, t);
        }
    }


}
